package com.shop.service.impl;

import com.github.pagehelper.PageInfo;
import com.shop.entity.Product;
import com.shop.mapper.ProductMapper;
import com.shop.service.ProductService;
import com.shop.vo.ProductAggregationVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("productService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class,
        propagation = Propagation.REQUIRED)
public class ProductServiceImpl implements ProductService {
    private final ProductMapper productMapper;
    /**
     * 获取商品列表
     * @param pageNum 当前页码
     * @param pageSize 每页数量
     * @return 商品列表
     */
    @Override
    public PageInfo<Product> listProduct(Integer pageNum, Integer pageSize) {
        List<Product> list = productMapper.listProduct(pageNum, pageSize);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Product> listProductCategoryMappings(Integer pageNum, Integer pageSize) {
        List<Product> list = productMapper.listProductCategoryMappings(pageNum, pageSize);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<ProductAggregationVO> listProductAggregation(int pageNum, int pageSize) {
        return new PageInfo<>(productMapper.listProductAggregation(pageNum, pageSize));
    }

    @Override
    public PageInfo<ProductAggregationVO> listAllProduct() {
        return new PageInfo<>(productMapper.listAllProduct());
    }

    /**
     * 根据id获取商品
     * @param id 商品id
     * @return 商品
     */
    @Override
    public ProductAggregationVO getProductById(Integer id) {
        return productMapper.getProductById(id);
    }
    /**
     * 添加商品
     * @param product 商品
     */
    @Override
    @Transactional
    public Integer addProduct(Product product) {
        productMapper.addProduct(product);
        return product.getId();
    }
    /**
     * 根据id删除商品
     * @param id 商品id
     */
    @Override
    public void deleteProduct(Integer id) {
        productMapper.deleteProduct(id);
    }
    /**
     * 更新商品
     * @param product 商品
     */
    @Override
    public void updateProduct(Product product) {
        productMapper.updateProduct(product);
    }
    /**
     * 获取商品总数
     * @return 商品总数
     */
    @Override
    public int getTotal() {
        return productMapper.getTotal();
    }
}
